#!/usr/bin/env python

import os
import sys
import ecore
import ecore.evas
import edje

from lists import TraitsList, CharacterList
from data.loader import load_character, list_characters
#from data.gurpsdata import Character

class GurpsCharacterGui(object):

    def __init__(self):
        edje.frametime_set(1.0/50)

        if ecore.evas.engine_type_supported_get("software_x11_16"):
            Engine = ecore.evas.SoftwareX11_16
        else:
            Engine = ecore.evas.SoftwareX11

        self.ee = Engine(w=800, h=480)
        self.ee.fullscreen = False

        # Load and setup UI
        self.ee.title = 'GURPS TL9'
        self.ee.name_class = ('GURPSTL9', 'GURPSTL9')
        self.canvas = self.ee.evas
        self.edje_file = os.path.join(os.path.dirname(sys.argv[0]), 'gurpstl9.edj')

        try:
            self.main_group = edje.Edje(self.canvas, file=self.edje_file, group='main')
        except Exception, e: # should be EdjeLoadError, but it's wrong on python2.5
            raise SystemExit('Failed to load Edje file: %s' % self.edje_file)

        self.main_group.size = self.canvas.size
        self.ee.data['edje'] = self.main_group
        self.ee.callback_resize = self.resize_cb

        self.main_group.on_key_down_add(self.key_down_cb, self.ee)

        self.main_group.show()

        #self.main_group.signal_callback_add('selected', '*', icon_selected)

        #Give focus to object, show window and enter event loop
        self.main_group.focus = True
        self.ee.show()

    # Setup callbacks for resize, keydown and selected item
    def resize_cb(self, ee):
        ee.data['edje'].size = ee.evas.rect.size

    def key_down_cb(self, bg, event, ee):
        k = event.key
        if k == 'Escape':
            ecore.main_loop_quit()
        if k in ('F6', 'f'):
            ee.fullscreen = not ee.fullscreen

    #def icon_selected(self, main_group, signal, source):
    #    print 'icon_selected', source

    def set_character_dir(self, chardir):
        # Character List
        charlist = list_characters(chardir)
        self.charlist_obj = CharacterList(self.canvas, file=self.edje_file)

        self.charlist_obj.freeze()

        chardesc = 'Professional adventurer, amateur alebrewer.'
        for charname, charpts, charfile in charlist:
            self.charlist_obj.row_add(charname, chardesc, charpts)

        self.charlist_obj.thaw()

        self.main_group.part_swallow('charlist', self.charlist_obj)

    def set_character(self, pc):

        def do_set_primary_attributes(pc):
            self.main_group.part_text_set('st1_value', str(pc.st))
            self.main_group.part_text_set('dx1_value', str(pc.dx))
            self.main_group.part_text_set('iq1_value', str(pc.iq))
            self.main_group.part_text_set('ht1_value', str(pc.ht))
            self.main_group.part_text_set('st2_value', str(pc.st))
            self.main_group.part_text_set('dx2_value', str(pc.dx))
            self.main_group.part_text_set('iq2_value', str(pc.iq))
            self.main_group.part_text_set('ht2_value', str(pc.ht))

        def do_set_secondary_attributes(pc):
            self.main_group.part_text_set('hp_value', str(pc.hp))
            self.main_group.part_text_set('will_value', str(pc.will))
            self.main_group.part_text_set('per_value', str(pc.per))
            self.main_group.part_text_set('fp_value', str(pc.fp))

        def do_set_points(pc):
            self.main_group.part_text_set('points_label', str(pc.points_total))

        def do_set_advantages(pc):
            adv_listobj = TraitsList(self.canvas, file=self.edje_file)
            adv_listobj.freeze()
            for adv in pc.advantages:
                adv_listobj.row_add(str(adv), 'images/close_btn.png')
            adv_listobj.thaw()
            self.main_group.part_swallow("advbar_list", adv_listobj)

        def do_set_disadvantages(pc):
            dadv_listobj = TraitsList(self.canvas, file=self.edje_file)
            dadv_listobj.freeze()
            for dadv in pc.disadvantages:
                dadv_listobj.row_add(str(dadv), 'images/close_btn.png')
            dadv_listobj.thaw()
            self.main_group.part_swallow("disadvbar_list", dadv_listobj)

        def do_set_skills(pc):
            skill_listobj = TraitsList(self.canvas, file=self.edje_file)
            skill_listobj.layer_set(-200)
            skill_listobj.freeze()
            for skill in pc.skills:
                skill_listobj.row_add(str(skill), 'images/close_btn.png')
            skill_listobj.thaw()
            self.main_group.part_swallow("skillbar_list", skill_listobj)

        self.main_group.part_text_set('charname_label', pc.name)
        do_set_primary_attributes(pc)
        do_set_secondary_attributes(pc)
        do_set_advantages(pc)
        do_set_disadvantages(pc)
        do_set_skills(pc)

    def main(self):
        ecore.main_loop_begin()


def main():
    gcg = GurpsCharacterGui()
    gcg.set_character_dir('./data')
    pcfile = os.path.join('data', 'pc.xml')
    gcg.set_character(load_character(pcfile))
    gcg.main()


if __name__ == '__main__':
    main()

